<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+ 新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name"></el-table-column>
            <el-table-column label="描述" prop="txt"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="setPower(row)"> 分配角色</el-button>
                  <el-button type="text" @click="edit(row)"> 修改</el-button>
                  <el-button type="text" @click="del(row)"> 删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <el-pagination
              @size-change="SizeChange"
              @current-change="CurrentChange"
              :current-page="page.page"
              :page-sizes="[1, 10, 100, 200]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
            >
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
         <component :is="'Info'"></component>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <DialogItem
      :isShow.sync="show"
      :list="list"
      @CurrentChange="CurrentChange"
      @getDate="getDate"
      ref="dialogItem"
    ></DialogItem>
    <!-- 分配权限 -->
<DialogTree :isShow.sync="showTree" ref="DialogTree"></DialogTree>
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import DialogItem from './components/dialog.vue'
import DialogTree from './components/dialogTree.vue'
export default {
  components: {
    DialogItem,
    DialogTree,
    Info:()=>import('./components/info.vue')
  },
  data () {
    return {
      show: false,
      showTree:false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100
      },
      list: []
    }
  },
  created () {
    this.getDate()
  },
  methods: {
    async getDate () {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.list = res.data.rows
      console.log('角色列表', res)
    },
    //页码改变
    CurrentChange (page) {
      this.page.page = page
      this.getDate()
    },
    //页容量改变
    SizeChange (size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getDate()
    },
    //新增角色
    async add () {
      this.show = true
    },
    //删除
    del (row) {
      this.$confirm('确定删除该角色吗', '提示')
        .then(async () => {
          await sysRoleDel(row.id)
          this.$message.success('删除成功')
          this.CurrentChange(1)
        })
        .catch(() => {})
    },
    //编辑
    //打开弹框，数据回显，加入一个参数用于区分新增编辑，表单验证。调用接口，完成关闭弹窗刷新数据
    edit (row) {
      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
      this.$refs.dialogItem.mode = 'edit'
    },
    //分配权限
    setPower(row){
      this.showTree=true
      this.$refs.DialogTree.getRoleInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;
}
.el-input {
  width: 40%;
}
.pagination {
  text-align: right;
  padding-top: 20px;
}
</style>
